package com.zengqingfa.interview;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zengqingfa
 * @className ABCTest7
 * @description
 * @create 2022/12/3 11:05
 */
public class ABCTest7 {
    //因为先执行线程A，所以这里设s1的计数器为1
    private static Semaphore semaphore1 = new Semaphore(1);

    private static Semaphore semaphore2 = new Semaphore(0);

    private static Semaphore semaphore3 = new Semaphore(0);


    /**
     * 打印ABC
     *
     * @param currentThread
     * @param nextThread
     */
    public void printABC(Semaphore currentThread, Semaphore nextThread) {
        for (int i = 0; i < 10;i++ ) {
            try {
                //阻塞当前线程获取
                currentThread.acquire();
                System.out.println(Thread.currentThread().getName());
                nextThread.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) throws InterruptedException {
        ABCTest7 abcTest7 = new ABCTest7();
        new Thread(() -> {
            abcTest7.printABC(semaphore1, semaphore2);
        }, "A").start();
        new Thread(() -> {
            abcTest7.printABC(semaphore2, semaphore3);
        }, "B").start();
        new Thread(() -> {
            abcTest7.printABC(semaphore3, semaphore1);
        }, "C").start();
    }


}